//
//  TrainingProjectViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/20.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "TrainingProjectViewController.h"
#import "SpecificTrainingViewController.h"
#import "SpineTestViewController.h"
#import "APIService.h"

@interface TrainingProjectViewController ()

@property (nonatomic, strong) UIButton * neckTrainingButton;
@property (nonatomic, strong) UIButton * waistTrainingButton;
@property (nonatomic, strong) UIButton * kneeTrainingButton;

@property (nonatomic, strong) RACCommand * neckTrainingCommand;
@property (nonatomic, strong) RACCommand * waistTrainingCommand;
@property (nonatomic, strong) RACCommand * kneeTrainingCommand;

@end

@implementation TrainingProjectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    self.title = @"锻炼项目";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.neckTrainingButton];
    [self.view addSubview:self.waistTrainingButton];
    [self.view addSubview:self.kneeTrainingButton];
    
    self.neckTrainingButton.rac_command = self.neckTrainingCommand;
    self.waistTrainingButton.rac_command = self.waistTrainingCommand;
    self.kneeTrainingButton.rac_command = self.kneeTrainingCommand;
}

- (void)bindViewModel
{
    [super bindViewModel];
}

- (void)makeConstaints
{
    [super makeConstaints];
    WS(weakSelf);
    [self.neckTrainingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight - TabBarHeight)/3));
        make.width.equalTo(@(screenWidth));
        make.top.equalTo(@0);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
    [self.waistTrainingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight - TabBarHeight)/3));
        make.width.equalTo(@(screenWidth));
        make.top.equalTo(weakSelf.neckTrainingButton.mas_bottom);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
    [self.kneeTrainingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((screenHeight - TabBarHeight)/3));
        make.width.equalTo(@(screenWidth));
        make.top.equalTo(weakSelf.waistTrainingButton.mas_bottom);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];
}

- (void)pushView:(NSInteger)trainingPart
{
    SpecificTrainingViewModel * viewModel = [[SpecificTrainingViewModel alloc] init];
    viewModel.trainingPart = trainingPart;
    switch (trainingPart) {
        case 0:
        {
            @weakify(self);
            if ([APIService shareService].spineTestData.neckPoint == 100) {
                UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"请先完成颈椎评估测试"
                                                                     message:nil
                                                                    delegate:nil
                                                           cancelButtonTitle:@"取消"
                                                           otherButtonTitles:@"确定", nil];
                [alertView.rac_buttonClickedSignal subscribeNext:^(NSNumber * x) {
                    @strongify(self);
                    if ([x integerValue] == 1) {
                        SpineTestViewModel * viewModel = [[SpineTestViewModel alloc] init];
                        viewModel.testType = TESTTYPECERVICALVERTEBRA;
                        SpineTestViewController * vc = [[SpineTestViewController alloc] initWithViewModel:viewModel];
                        [vc createCervicalVertebraQuestionaire];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }];
                [alertView show];
            }
            else {
                if ([APIService shareService].spineTestData.neckPoint > 3) {
                    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"您的颈椎受损程度较高，建议前往医院进行治疗"
                                                                         message:nil
                                                                        delegate:nil
                                                               cancelButtonTitle:@"确定"
                                                               otherButtonTitles:nil, nil];
                    [alertView show];
                }
                else {
                    viewModel.severity = [APIService shareService].spineTestData.neckPoint;
                    SpecificTrainingViewController * vc = [[SpecificTrainingViewController alloc] initWithViewModel:viewModel];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        }
            break;
        case 1:
        {
            @weakify(self);
            if ([APIService shareService].spineTestData.waistPoint == 100) {
                UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"请先完成腰椎评估测试"
                                                                     message:nil
                                                                    delegate:nil
                                                           cancelButtonTitle:@"取消"
                                                           otherButtonTitles:@"确定", nil];
                [alertView.rac_buttonClickedSignal subscribeNext:^(NSNumber * x) {
                    @strongify(self);
                    if ([x integerValue] == 1) {
                        SpineTestViewModel * viewModel = [[SpineTestViewModel alloc] init];
                        viewModel.testType = TESTTYPELUMBARVERTEBRA;
                        SpineTestViewController * vc = [[SpineTestViewController alloc] initWithViewModel:viewModel];
                        [vc createLumbarVertebraQuestionaire];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }];
                [alertView show];
            }
            else {
                if ([APIService shareService].spineTestData.waistPoint > 3) {
                    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"您的腰椎受损程度较高，建议前往医院进行治疗"
                                                                         message:nil
                                                                        delegate:nil
                                                               cancelButtonTitle:@"确定"
                                                               otherButtonTitles:nil, nil];
                    [alertView show];
                }
                else {
                    viewModel.severity = [APIService shareService].spineTestData.waistPoint;
                    SpecificTrainingViewController * vc = [[SpecificTrainingViewController alloc] initWithViewModel:viewModel];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        }
            break;
        case 2:
        {
            [MBProgressHUD showQuickTipWithTitle:nil withText:@"暂未开放"];
            //viewModel.severity = [APIService shareService].spineTestData.kneePoint;
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - Getter
- (UIButton *)neckTrainingButton
{
    if (!_neckTrainingButton) {
        _neckTrainingButton = [[UIButton alloc] init];
        [_neckTrainingButton setTitle:@"颈部锻炼项目" forState:UIControlStateNormal];
        [_neckTrainingButton setTitleColor:[UIColor colorWithRGBHex:0x24B999] forState:UIControlStateNormal];
        [_neckTrainingButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _neckTrainingButton;
}

- (UIButton *)waistTrainingButton
{
    if (!_waistTrainingButton) {
        _waistTrainingButton = [[UIButton alloc] init];
        [_waistTrainingButton setTitle:@"腰部锻炼项目" forState:UIControlStateNormal];
        [_waistTrainingButton setTitleColor:[UIColor colorWithRGBHex:0x24B999] forState:UIControlStateNormal];
        [_waistTrainingButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _waistTrainingButton;
}

- (UIButton *)kneeTrainingButton
{
    if (!_kneeTrainingButton) {
        _kneeTrainingButton = [[UIButton alloc] init];
        [_kneeTrainingButton setTitle:@"足膝锻炼项目" forState:UIControlStateNormal];
        [_kneeTrainingButton setTitleColor:[UIColor colorWithRGBHex:0x24B999] forState:UIControlStateNormal];
        [_kneeTrainingButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    }
    return _kneeTrainingButton;
}

- (RACCommand *)neckTrainingCommand
{
    if (!_neckTrainingCommand) {
        @weakify(self);
        _neckTrainingCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            [self pushView:0];
            return [RACSignal empty];
        }];
    }
    return _neckTrainingCommand;
}

- (RACCommand *)waistTrainingCommand
{
    if (!_waistTrainingCommand) {
        @weakify(self);
        _waistTrainingCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            [self pushView:1];
            return [RACSignal empty];
        }];
    }
    return _waistTrainingCommand;
}

- (RACCommand *)kneeTrainingCommand
{
    if (!_kneeTrainingCommand) {
        @weakify(self);
        _kneeTrainingCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            [self pushView:2];
            return [RACSignal empty];
        }];
    }
    return _kneeTrainingCommand;
}

#pragma mark - dealloc
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
